<?php


require_once(DIR_LIB."vslibdef.php");

if(!defined('DIR_LIB_VSCORE'))
	define('DIR_LIB_VSCORE', DIR_LIB."VsCore".DIR_SEP);

if(!defined('DIR_LIB_VSSHOP'))
	define('DIR_LIB_VSSHOP', DIR_LIB."VsShop".DIR_SEP);


require_once(DIR_LIB_VSCORE."vsapplication.php");
require_once(DIR_LIB_VSCORE."vsabstractfactory.php");

require_once(DIR_LIB_VSSHOP."vsproductcategory.php");

class VsProductCategoriesFactory implements VsAbstractFactory {
	private static $_Instance = NULL;

	private $_app = NULL;

	private static $_sqlTable='products_categories';

	private $_pager = NULL;

	private $_parent = NULL;
	private $_selected = NULL;
	private $_listItems = array();

	static function Instance() {
		if(!self::$_Instance)
			self::$_Instance = &new VsProductCategoriesFactory();

		return self::$_Instance;
	}
	//===============================================================================

	function setPager(&$pager) { $this->_pager = $pager; }
	//==============================================================================

	function makeCategoryPaths($parentId=0, $parentPath='') {
		
		$_listCategories = array();
		$paths = array();

		$db = $this->_app->db();
		$q = "SELECT * FROM `".self::$_sqlTable."` WHERE parent_id=".$parentId;
		$res = $db->query($q);

		while($rCat = @mysql_fetch_object($res)) {
			$path = $parentPath."/".stripslashes($rCat->name);
			array_push($_listCategories, array('id'=>$rCat->id, 'path'=>$path));
			array_merge($_listCategories, $this->makeCategoryPaths($rCat->id, $path));
		}

		return $_listCategories;
	}
	//===============================================================================

	function itemsCount($parent=0, $onlyEnabled=FALSE, $recursivly=FALSE) {

		$q = "SELECT COUNT(id) as count FROM `".self::$_sqlTable."`";
		if($parent || $recursivly)
			$q .= " WHERE parent_id=".$parent;

		//echo $q;

		$db = $this->_app->db();
		$res = $db->query($q);
		$r = mysql_fetch_assoc($res);

		return $r['count'];
	}
	//==============================================================================

	function makeItems($parent=0, $onlyEnabled=FALSE, $recursivly=FALSE) {
		$db = $this->_app->db();
		$listCategories = array();

		if(($parent<6))
			return $listCategories;

		//if(defined("VS_MULTILANG"))


		$q = "SELECT * FROM `".self::$_sqlTable."`";
		if($parent || $recursivly)
			$q .= " WHERE parent_id=".$parent;

		$q .= " ORDER BY order_by";

		if($this->_pager)
			$q .= " LIMIT ".$this->_pager->from().", ".$this->_pager->view();
		//echo $q;

		$res = $db->query($q);
		while($rCat = @mysql_fetch_object($res)) {
			if($onlyEnabled && (!$rCat->enabled))
				continue;

			$cat = new VsProductCategory();

			$cat->_id = $rCat->id;
			$cat->_parent = $rCat->parent_id;
			$cat->_name = stripslashes($rCat->name);
			$cat->_date = $rCat->date;

			$cat->_orderBy = $rCat->order_by;
			$cat->_enabled = $rCat->enabled;

			$cat->_global = $rCat->global;

			array_push($listCategories, $cat);
		}

		return $listCategories;
	}
	//==================================================================================

	function makeRootItem() {
		$cat = new VsProductCategory();

		$cat->_id = 0;
		$cat->_name = "Root";

		return $cat;
	}
	//====================================================================================

	function makeNewItem($parent=0) {
		$cat = new VsProductCategory();

		$cat->_id = 0;
		$cat->_parent = $parent;
		$cat->_name = "";

		return $cat;
	}
	//====================================================================================

	function makeItem($id) {
		if($id < 1)
			return $this->makeRootItem();

		//if(($id==6)||($id==7))
		//	return $this->makeNewItem($id);

		$db = $this->_app->db();
		$listCategories = array();

		$q = "SELECT * FROM `".self::$_sqlTable."` WHERE id=".$id;
		//echo $q;
		//exit;

		$res = $db->query($q);
		$rCat=mysql_fetch_object($res);
		$cat = new VsProductCategory();

		if($rCat) {
			$cat->_id = $rCat->id;
			$cat->_parent = $rCat->parent_id;
			$cat->_name = stripslashes($rCat->name);
			$cat->_date = $rCat->date;

			$cat->_orderBy = $rCat->order_by;
			$cat->_enabled = $rCat->enabled;

			$cat->_global = $rCat->global;
		}

		return $cat;
	}
	//===============================================================================

	function search($value, $key, $type='LIKE') {

	}
	//===============================================================================

	private function __construct() {
		$this->_app = VsApplication::Instance();

		//self::$_sqlTable = self::$_sqlTable."_".strtolower(VsSession::Instance()->value('lang'));
		self::$_sqlTable = self::$_sqlTable."_bg";
	}
	//==================================================================================

	private function install() {


	}
	//===================================================================================

};

?>
